<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <!-- <el-col :span="12">
            <el-form-item label="员工编号" prop="workNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.workNumber" placeholder="请填写员工编号"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="姓名" prop="name"
                :rules="[
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写姓名"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="单位" prop="unit"
                :rules="[
                 ]">
              <el-input v-model="inputForm.unit" placeholder="请填写单位"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="职务" prop="currentPosition"
                :rules="[
                 ]">
              <el-input v-model="inputForm.currentPosition" placeholder="请填写职务"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="岗位类别" prop="identity"
                :rules="[
                 ]">
              <el-input v-model="inputForm.identity" placeholder="请填写岗位类别"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="票类别" prop="ticket"
                :rules="[
                 ]">
               <el-checkbox-group v-model="ticket">
                 <el-checkbox-button v-for="item in tickets" :label="item" :key="item">{{item}}</el-checkbox-button>
               </el-checkbox-group>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="照片" prop="photo"
                :rules="[
                 ]">
              <el-input v-model="inputForm.photo" placeholder="请填写照片"     ></el-input>
           </el-form-item>
        </el-col> -->
        <!-- <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col> -->
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          name: '',
          unit: '',
          currentPosition: '',
          identity: '',
          photo: '',
          workNumber: '',
          ticket: '',
          remarks: '',
          createData: '',
          deleteData: '',
          status: '',
          userId: ''
        },
        oldStr: '',
        newStr: '',
        ticket: [],
        tickets: [
          '所有中层干部(校领导票)',
          '教辅中层干部(服务对象票)',
          '教学教辅中层干部(同级票)',
          '机关中层干部(服务对象票)',
          '机关中层干部(同级票)',
          '中层干部(群众票)'
        ]
      }
    },
    components: {
    },
    methods: {
      init (method, id) {
        this.ticket = []
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建考核人员信息`
        } else if (method === 'edit') {
          this.title = '修改考核人员信息'
        } else if (method === 'view') {
          this.title = '查看考核人员信息'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/peopleinformation/peopleassessdata/peopleAssessInformation/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.peopleAssessInformation)
              for (let i in this.inputForm.ticket.split(',')) {
               if (this.inputForm.ticket.split(',')[i] !== '') {
                 this.ticket.push(this.inputForm.ticket.split(',')[i])
               }
              }
              this.inputForm.createData = this.inputForm.ticket
              this.loading = false
            })
          }
        })
      },
      // 比较插入与删除
      compare () {
        var oldArr = this.inputForm.createData.split(',')
        var newArr = this.inputForm.ticket.split(',')
        var deleteArr = []
        var insertArr = []
        for (let i in oldArr) {
            if (newArr.indexOf(oldArr[i]) === -1 && oldArr[i] !== '') {
              deleteArr.push(oldArr[i])
            }
        }
        for (let i in newArr) {
            if (oldArr.indexOf(newArr[i]) === -1 && newArr[i] !== '') {
              insertArr.push(newArr[i])
            }
        }
        if (insertArr.length !== 0 && deleteArr.length !== 0) {
          this.inputForm.status = 0
        } else if (insertArr.length !== 0) {
          this.inputForm.status = 1
        } else if (deleteArr.length !== 0) {
          this.inputForm.status = 2
        }
        this.inputForm.createData = this.$ticketTableName.tableName(insertArr)
        this.inputForm.deleteData = this.$ticketTableName.tableName(deleteArr)
      },
      // 表单提交
      doSubmit () {
        this.inputForm.ticket = ''
        for (let i in this.ticket) {
          if (i === '0') {
            this.inputForm.ticket += this.ticket[i]
          } else {
            this.inputForm.ticket += ',' + this.ticket[i]
          }
        }
        this.compare()
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/peopleinformation/peopleassessdata/peopleAssessInformation/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
